﻿using System;
using System.Collections;


namespace Chess_Model
{

    public class Pawn : Piece
    {
        //flag to check if it's the first move
        bool first;
        //playermove used to see if pawn is moving forward or backward 
        int playermove;

        public Pawn(Position position, int play)
        {
            first = true;
            pos = position;
            player = play;
        }
       
        // Generates a list of new legal positions from the current position
        // regardless of the other pieces' positions
        public override ArrayList getNextLegalMoves()
        {
            //this section make sure you are going to be moving in the correct direction
            //if it's a white piece will move up one
            //else if it's a black piece it will move down one
            if (player == 1)
                playermove = 1;
            else
                playermove = -1;

            ArrayList mylist = new ArrayList();
            int xcoord, ycoord;
            xcoord = pos.x;
            ycoord = pos.y;
            Position[] ListofMoves = new Position[3];

            ycoord = ycoord + playermove;//this either moves the piece up or down one
            ListofMoves[0] = new Position(xcoord, ycoord);
            ListofMoves[1] = new Position(xcoord - 1, ycoord);
            ListofMoves[2] = new Position(xcoord + 1, ycoord);

            for(int i = 0; i < 3; i++)
            {
                //if it is not out of bounds then add
                if (!((ListofMoves[i].x < 0) || (ListofMoves[i].x > 7) || (ListofMoves[i].y < 0) || (ListofMoves[i].y > 7)))
                    mylist.Add(ListofMoves[i]);
            }

            if (first)
            {
                ycoord = ycoord + playermove;
                if ((ycoord > 0) && (ycoord < 7))
                    mylist.Add(new Position(xcoord, ycoord));

            }

            return mylist;
  
        }

        
        //use to change first to false when pawn makes it's move
        public override bool moveTo(Position newPos)
        {
            if (newPos == null || !ChessUtilities.isInRange(newPos.x, newPos.y))
                return false;

            pos.x = newPos.x;
            pos.y = newPos.y;

            first = false;//once it has moved this will be false so it can change available moves

            return true;
        }

        public override String toString()
        {
            return String.Format("{0}{1}", "P", player);
        }



    }

}
